package com.park.platform.service.impl;

import com.park.common.bean.CommentInputView;
import com.park.common.bean.PageBean;
import com.park.common.constant.IDBConstant;
import com.park.common.poo.CommentInfo;
import com.park.common.poo.ReplyInfo;
import com.park.common.util.DateUtil;
import com.park.common.util.JsonUtils;
import com.park.common.util.StrUtil;
import com.park.platform.dao.IBaseDao;
import com.park.platform.service.ICommentService;
import com.park.platform.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-04-30.
 */
@Service
public class CommentServiceImpl extends BaseService implements ICommentService {

    @Autowired
    private IBaseDao baseDao;

    @Autowired
    private IDictService dictService;

    @Override
    public PageBean getComments(CommentInputView commentInputView){

        Integer serviceId = commentInputView.getServiceId();
        String serviceType = commentInputView.getServiceType();

        StringBuilder headSql = new StringBuilder("SELECT DATE_FORMAT(c.createTime, '%Y-%m-%d') createTime, c.*, u.operatorName, u.headImg");
        StringBuilder bodySql = new StringBuilder(" FROM comment_info c, user_operator u");
        StringBuilder whereSql = new StringBuilder(" WHERE c.id = u.id AND commentStatus = ").append(IDBConstant.LOGIC_STATUS_YES);
        if(serviceId != null){
            whereSql.append(" AND serviceId = :serviceId");
        }
        if(StrUtil.isNotBlank(serviceType)){
            whereSql.append(" AND serviceType = :serviceType");
        }
        whereSql.append(" ORDER BY c.createTime DESC");

        PageBean pageBean = super.getPageBean(headSql, bodySql, whereSql, commentInputView);
        List<Map<String, Object>> list = pageBean.getList();
        for(Map<String, Object> map : list){
            map.put("replys", getReplys(StrUtil.objToInt(map.get("commentId"))));
        }

        return pageBean;
    }

    private List<Map<String, Object>> getReplys(int commentId){
        return baseDao.queryBySql("SELECT * FROM reply_info WHERE commentId = ? AND replyStatus = " + IDBConstant.LOGIC_STATUS_YES,  commentId);
    }

    @Override
    public void newComment(CommentInfo commentInfo){
        commentInfo.setCreateTime(DateUtil.getNowDate());
        commentInfo.setCommentStatus(IDBConstant.LOGIC_STATUS_YES);
        baseDao.save(commentInfo, null);
    }

    @Override
    public int getCommentCount(CommentInputView commentInputView){
        Integer serviceId = commentInputView.getServiceId();
        String serviceType = commentInputView.getServiceType();

        StringBuilder sql = new StringBuilder("SELECT COUNT(1) FROM comment_info c WHERE commentStatus = ").append(IDBConstant.LOGIC_STATUS_YES);
        if(serviceId != null){
            sql.append(" AND serviceId = :serviceId");
        }
        if(StrUtil.isNotBlank(serviceType)){
            sql.append(" AND serviceType = :serviceType");
        }
        return baseDao.getUniqueResult(sql.toString(), JsonUtils.fromJson(commentInputView)).intValue();
    }

    /*********************************************************************************************************************/

    @Override
    public PageBean getCommentsIN(CommentInputView commentInputView){

        Integer serviceId = commentInputView.getServiceId();
        String serviceType = commentInputView.getServiceType();
        String commentStatus = commentInputView.getCommentStatus();
        String isReply = commentInputView.getIsReply();

        StringBuilder headSql = new StringBuilder("SELECT c.*, u.operatorName, u.headImg, IFNULL((SELECT 1 FROM reply_info r WHERE r.commentId=c.commentId LIMIT 1), 2) isReply");
        StringBuilder bodySql = new StringBuilder(" FROM comment_info c, user_operator u");
        StringBuilder whereSql = new StringBuilder(" WHERE c.id = u.id");
        if(StrUtil.isNotBlank(commentStatus)){
            whereSql.append(" AND commentStatus = :commentStatus ");
        }
        if(serviceId != null){
            whereSql.append(" AND serviceId = :serviceId");
        }
        if(StrUtil.isNotBlank(serviceType)){
            whereSql.append(" AND serviceType = :serviceType");
        }
        if(StrUtil.isNotBlank(isReply)){
            whereSql.append(" AND IFNULL((SELECT 1 FROM reply_info r WHERE r.commentId=c.commentId LIMIT 1), 2) = :isReply");
        }
        whereSql.append(" ORDER BY c.createTime DESC");

        PageBean pageBean = super.getPageBean(headSql, bodySql, whereSql, commentInputView);
        List<Map<String, Object>> list = pageBean.getList();
        for(Map<String, Object> map : list){
            map.put("serviceTypeName", dictService.getDictValueByNameKey(IDBConstant.COMMENT_SERVICE_TYPE, StrUtil.objToStr(map.get("serviceType"))));
        }

        return pageBean;
    }

    @Override
    public void newDeleteComment(int commentId) {
        CommentInfo commentInfo = getCommentInfo(commentId);
        if(IDBConstant.LOGIC_STATUS_NO.equals(commentInfo.getCommentStatus())){
            baseDao.delete(commentInfo);
        }else {
            commentInfo.setCommentStatus(IDBConstant.LOGIC_STATUS_NO);
            baseDao.save(commentInfo, commentInfo.getCommentId());
        }
    }

    @Override
    public CommentInfo getCommentInfo(int commentId) {
        return baseDao.get(CommentInfo.class, commentId);
    }

    @Override
    public Map<String, Object> getCommentInfoAndReplyInfo(int commentId){
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("comment", getCommentInfo(commentId));
        resultMap.put("reply", getReplyInfo(commentId));
        return resultMap;
    }

    @Override
    public void saveCommentReply(ReplyInfo replyInfo){
        ReplyInfo replyInfoDB = getReplyInfo(replyInfo.getCommentId());
        if(replyInfoDB != null){
            baseDao.delete(replyInfoDB);
            if(StrUtil.isBlank(replyInfo.getReplyContent())) return;
        }
        if(StrUtil.isBlank(replyInfo.getReplyContent())) return;
        replyInfo.setCreateTime(DateUtil.getNowDate());
        replyInfo.setReplyStatus(IDBConstant.LOGIC_STATUS_YES);
        baseDao.save(replyInfo, null);
    }

    private ReplyInfo getReplyInfo(int commentId) {
        return baseDao.queryByHqlFirst("FROM ReplyInfo WHERE commentId = ?", commentId);
    }

}
